/*
 * Copyright (C) 2017
 *
 * Brick Yang <printfxxx@163.com>
 *
 * This program is free software. You can redistribute it and/or
 * modify it as you like.
 */

/**
 * @file	dprc.c
 * @brief	dprc driver
 */

#include <mtrace.h>

#include "dprc.h"

#define dprc_cmd_get_connection		__dprc_cmd_get_connection
#define dprc_rsp_get_connection		__dprc_rsp_get_connection

/* Command IDs */
#define __DPRC_CMDID_GET_CONNECTION	DPRC_CMD(0x16c)

struct dprc_cmd_get_connection {
	/* cmd word 0 */
	__le32 ep1_id;
	__le32 ep1_interface_id;
	/* cmd word 1-2 */
	u8 ep1_type[16];
};

struct dprc_rsp_get_connection {
	/* response word 0-2 */
	__le64 pad[3];
	/* response word 3 */
	__le32 ep2_id;
	__le32 ep2_interface_id;
	/* response word 4-5 */
	u8 ep2_type[16];
	/* response word 6 */
	__le32 state;
};

int dprc_get_connection(struct fsl_mc_io *mc_io,
			u32 cmd_flags,
			u16 token,
			const struct dprc_endpoint *ep1,
			struct dprc_endpoint *ep2,
			int *state)
{
	struct fsl_mc_command cmd = { 0 };
	struct dprc_cmd_get_connection *dprc_cmd;
	struct dprc_rsp_get_connection *dprc_rsp;
	int err;

	/* prepare command */
	cmd.header = mc_encode_cmd_header(__DPRC_CMDID_GET_CONNECTION,
					  cmd_flags,
					  token);
	dprc_cmd = (struct dprc_cmd_get_connection *)cmd.params;
	dprc_cmd->ep1_id = le32_to_cpu(ep1->id);
	dprc_cmd->ep1_interface_id = le32_to_cpu(ep1->if_id);
	strncpy(dprc_cmd->ep1_type, ep1->type, 16);

	/* send command to mc */
	err = mc_send_command(mc_io, &cmd);
	if (err)
		return err;

	/* retrieve response parameters */
	dprc_rsp = (struct dprc_rsp_get_connection *)cmd.params;
	ep2->id = le32_to_cpu(dprc_rsp->ep2_id);
	ep2->if_id = le32_to_cpu(dprc_rsp->ep2_interface_id);
	strncpy(ep2->type, dprc_rsp->ep2_type, 16);
	*state = le32_to_cpu(dprc_rsp->state);
	return 0;
}
